package com.dgut.courses_selecting_sys.controller;


import com.dgut.courses_selecting_sys.entity.AjaxResult;
import com.dgut.courses_selecting_sys.entity.Notice;
import com.dgut.courses_selecting_sys.service.IDeptNoticeService;
import com.dgut.courses_selecting_sys.service.INoticeService;
import com.dgut.courses_selecting_sys.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuec
 * @since 2020-09-09
 */
@Controller
@RequestMapping("//notice")
public class NoticeController {

    @Autowired
    INoticeService noticeService;
    @Autowired
    IDeptNoticeService deptNoticeService;

    @GetMapping("/history")
    public String history(){
        return "notice/history";
    }


    @GetMapping("/create")
    public String create(){
        return "notice/create";
    }

    @RequiresRoles(value = {"teacher", "admin"}, logical = Logical.OR)
    @ResponseBody
    @PostMapping("/createNotice")
    public AjaxResult createNotice(@Validated Notice notice){
        notice.setCreator(ShiroUtils.getSysUser().getUserName());
        notice.setFromUserid(ShiroUtils.getSysUser().getUserId());
        notice.setDeleted(0);
        try{
        deptNoticeService.createData(notice,ShiroUtils.getSysUser().getDeptId());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("公告通知设置出错");
        }
        return AjaxResult.success();
    }

    @ResponseBody
    @GetMapping("/history/NewTen")
    public AjaxResult history_NewTen(){
        Integer deptId = ShiroUtils.getSysUser().getDeptId();
        List<Notice> noticeList=noticeService.listNewTen(deptId);
        if(noticeList.size()==0){
            return AjaxResult.error("暂无公告通知数据");
        }
        return AjaxResult.success(noticeList);
    }

    @ResponseBody
    @GetMapping("/history/PassMonth")
    public AjaxResult history_PassMonth(){
        List<Notice> noticeList=noticeService.listPassMonth();
        if(noticeList.size()==0){
            return AjaxResult.error("暂无公告通知数据");
        }
        return AjaxResult.success(noticeList);
    }

    @ResponseBody
    @GetMapping("/history/AllNotice")
    public AjaxResult history_AllNotice(){
        List<Notice> noticeList=noticeService.listAllNotice(ShiroUtils.getSysUser().getDeptId());
        if(noticeList.size()==0){
            return AjaxResult.error("暂无公告通知数据");
        }
        return AjaxResult.success(noticeList);
    }

}
